package com.fjycnet.qiniu;

import android.widget.TextView;
import android.widget.Toast;

import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * ================================================
 * 作    者：Weirdo_lin
 * 版    本：1.0
 * 创建日期：2018/5/2
 * 描    述：
 * ================================================
 */
public class YcQiNiuUtil {

    public static void uploadSingle(final TextView textView, UploadManager mgr, String file, String token, final String domain, final MyQiniuListener listener) {
        String key = "weiai" + System.currentTimeMillis() + "";
        mgr.put(file, key, token, new UpCompletionHandler() {
            @Override
            public void complete(String key, ResponseInfo info, JSONObject response) {
                StringBuilder result = new StringBuilder();
                result.append("key=")
                        .append(String.valueOf(key))
                        .append("\nResponseInfo=" + String.valueOf(info))
                        .append("\nJSONObject=" + response);
                textView.setText(result.toString());
                if (info.error == null) {
                    String qiniuUrl = domain + "/" + key;
                    listener.request(qiniuUrl);
                } else {
                    listener.request("500");
                }
            }
        }, null);
    }

    public static void uploadMultiply(final UploadManager mgr, final List<String> file, final String token, final String domain, final MyCompleteListener listener) {
        final List<String> mResultList = new ArrayList<>();
        for (int i = 0; i < file.size(); i++) {
            final String url = file.get(i);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Random random = new Random();
                    int number = random.nextInt(600);
                    try {
                        mgr.put(url, String.valueOf(System.currentTimeMillis() + number), token, new UpCompletionHandler() {
                            @Override
                            public void complete(String key, ResponseInfo info, JSONObject response) {
                                if (info.error == null) {
                                    String qiniuUrl = domain + "/" + key;
                                    mResultList.add(qiniuUrl);

                                    if (file.size() == mResultList.size()) {
                                        listener.result(mResultList);
                                    }
                                } else {
                                    listener.faild(info.error);
                                }
                            }
                        }, null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }


    public interface MyQiniuListener {
        void request(String url);
    }

    public interface MyCompleteListener {
        void result(List<String> result);

        void faild(String errmsg);
    }
}